//index.js
//获取应用实例
var app = getApp();
Page({
  data:{
    tabId: 1,
    listData: [],
    page:1,
    page2: 1,
    loading:0,
    lastPage:0,
    lastPage2: 0,
    contactsData: [],
    loaded1:0,
    loaded2: 0,
    user_info: {},
    notiCount:{},
  },
  onLoad: function (options) {
    //console.log(app.globalData.window);
    var user_info = wx.getStorageSync('user_info');
    this.setData({ window: app.globalData.window })
    this.initData();
  },
  initData:function(reload=0){
    if (reload==1){
      this.setData({
        listData: [],
        page: 1,
        page2: 1,
        loading: 0,
        lastPage: 0,
        lastPage2: 0,
        loaded1:0,
        loaded2:0,
        contactsData: [],
      });
    }
    if (reload==0){
      this.getNotiCount();
    }
    
   if(this.data.tabId==0){
     if (reload == 2 && this.data.loaded1==1){
       return ; 
     }
     this.setData({ loaded1:1});
     this.getcontacts();
   }else{
     if (reload == 2 && this.data.loaded2 == 1) {
       return;
     }
     this.setData({ loaded2: 1 });
     this.getnotification();
   }
   
   
  },
  //提醒数
  getNotiCount(){
    var that = this;
    app.api("api/notification/allNoti",{},function(res){
        if(res.code!=200){
           app.alert(res.msg);
           return;
        }
       // console.log(res.data);
        that.setData({ notiCount:res.data});
       // console.log(that.data.notiCount);
    })
  },

  //提醒列表
  getnotification(){
    //通知提醒 
    var that = this;
    var page = this.data.page2;
    this.setData({
       loading:1
    });
    app.api("api/notification/get",{
      page: page,
      limit:20,
    }, function (res) {
      wx.stopPullDownRefresh();
      if (res.code != 200) {
       
        app.alert(res.msg);
        return;
      }
      that.delNoit("noti");
      if (res.data.list.length==0){
        that.setData({
          lastPage2: 1
        });
      }
    
      page++;
      if (res.data.list) {
        that.setData({ page2: page, loading: 0, 'listData': that.data.listData.concat(res.data.list) });
      }
    });
  },

  getcontacts(){
    //聊天记录
    var page = this.data.page;
    var that = this;
    app.api("api/message/contacts", {
      page: page,
      pagesize: 20,
    },function (res) {
      wx.stopPullDownRefresh();
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      if (res.data.length == 0) {
        that.setData({
          lastPage: 1
        });
      }
      page++;
      that.delNoit("msg");
      if (res.data) {
        that.setData({ page: page, 'contactsData':                    that.data.contactsData.concat(res.data) });
      }
    });
  },

  //删除提醒数
  delNoit(type){
    var that = this;
    var notiCount = this.data.notiCount;
    if (notiCount[type]>0){
      app.api("api/notification/del", { type: type }, function (res) {
        notiCount[type] = 0
        that.setData({ notiCount: notiCount });
      })
    }
   
  },
  scrolltolower:function(){
    this.initData();
  },
  indexTab: function (e) {
    var id = e.currentTarget.dataset.id;
    this.setData({ tabId: id });
    this.initData(2);
  },

  onPullDownRefresh: function () {
    this.initData(1);
  },
  swiperChange: function (e) {
    this.setData({ tabId: e.detail.current });
    this.initData(2);
  },
  jumpnoti:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    that.data.listData[id].status =1;
    var data = this.data.listData[id];    
    app.api("api/notification/add", {
      status: 1,
      id:data.id,
      method: 'POST',
    },function(res){
      that.setData({ listData: that.data.listData});
    });
    wx.navigateTo({
      url: data.url
    })
  },
  jumpChat:function(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    that.data.contactsData[id].noread = 0;
    var data = this.data.contactsData[id];
    app.api("api/message/read", {
      to_user_id: data.to_user_id,
      method: 'POST',
    }, function (res) {
      if(res.code==200){
        that.setData({ contactsData: that.data.contactsData });
      }
    });
  }


})
